package com.xcode.read;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.xcode.read.album.LocalImageManager;
import com.xcode.read.common.IFragment;
import com.xcode.read.userControl.tab.TabView;
import com.xcode.read.fragment.AboutMe;
import com.xcode.read.fragment.AlbumExplore;
import com.xcode.read.fragment.CaptureStatus;
import com.xcode.read.fragment.AppIntroduce;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends FragmentActivity {
    private String[] mTabText = {"阿拉伯", "关于"};
    private int[] mTabSelect = { R.mipmap.msg_select, R.mipmap.about_select};
    private int[] mTabNormal = {R.mipmap.msg_normal, R.mipmap.about_normal};
    private int mTabItemCount;
    private ViewPager mViewPager;
    private TabView mTabView;
    private Map<Integer, Fragment> mFragmentMap;
    private IFragment mIFragment;
    private View hideLayout, hideFunction1, hideFunction2, fragmentParentView, tipView;
    private int clickTimes = 0;
    private long clickTime;
    private long clickTime1 = 0;

    private String fragmentName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTabItemCount = mTabText.length;
        mFragmentMap = new HashMap(mTabItemCount);
        mViewPager = (ViewPager) findViewById(R.id.main_fragment_container);
        mViewPager.setOffscreenPageLimit(mTabItemCount);
        mViewPager.setAdapter(new PageAdapter(getSupportFragmentManager()));
        mViewPager.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickTimes == 0) {
                    clickTime = System.currentTimeMillis();
                    clickTimes++;
                    return;
                }

                if (Math.abs(clickTime - System.currentTimeMillis()) < 50) {
                    clickTime = System.currentTimeMillis();
                    clickTimes++;
                    if (clickTimes > 5) {

                    }
                    return;
                }
            }
        });
        mTabView = (TabView) findViewById(R.id.main_tabView);
        mTabView.setViewPager(mViewPager);
        mTabView.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mIFragment = ((IFragment) mFragmentMap.get(position));
                mIFragment.OnEnter();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        hideLayout = findViewById(R.id.hide_function);
        hideLayout.setVisibility(View.GONE);

        hideFunction1 = findViewById(R.id.hide_function1);
        hideFunction2 = findViewById(R.id.hide_function2);

        hideFunction1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addFragment(1);
                hideLayout.setVisibility(View.GONE);
            }
        });
        hideFunction2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addFragment(2);
                hideLayout.setVisibility(View.GONE);
            }
        });

        fragmentParentView = findViewById(R.id.content_frame);

        clickTime1 = 0;
        tipView = findViewById(R.id.tip_view);
        tipView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Math.abs(clickTime1 - System.currentTimeMillis()) < 1000) {
                    hideLayout.setVisibility(View.VISIBLE);
                }
                clickTime1 = System.currentTimeMillis();
            }
        });
    }

    private void addFragment(int code) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if(TextUtils.isEmpty(fragmentName)) {
            fragmentParentView.setVisibility(View.VISIBLE);
            fragmentParentView.setBackgroundColor(getResources().getColor(android.R.color.white));
        } else {
            Fragment removeFragment = getSupportFragmentManager().findFragmentByTag(fragmentName);
            if(removeFragment != null && !removeFragment.isDetached()) {
                ft.detach(removeFragment);
                ft.remove(removeFragment);
            }
        }
        Fragment fragment;
        if(code == 1) {
            fragment = new AlbumExplore();
        } else {
            fragment = new AppIntroduce();
        }
        ((IFragment)fragment).OnEnter();
        fragmentName = fragment.getClass().getName();
        ft.add(R.id.content_frame, fragment, fragmentName);
        ft.commitAllowingStateLoss();
    }

    private void removeFragment() {
        if(TextUtils.isEmpty(fragmentName)) {
            return;
        }

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        Fragment removeFragment = getSupportFragmentManager().findFragmentByTag(fragmentName);
        if(removeFragment != null && !removeFragment.isDetached()) {
            ft.detach(removeFragment);
            ft.remove(removeFragment);
            ft.commitAllowingStateLoss();
        }
        fragmentName = null;
        fragmentParentView.setVisibility(View.GONE);
    }

    @Override
    public void onBackPressed() {
        if(View.VISIBLE == fragmentParentView.getVisibility()) {
            removeFragment();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case AlbumExplore.REQUEST_CODE_VIEW_ALBUM: {
                if (LocalImageManager.GetInstance().RefreshImageList())
                    mIFragment.OnEnter();
            }
            break;

            default:
                super.onActivityResult(requestCode, resultCode, data);
        }
    }

    class PageAdapter extends FragmentPagerAdapter implements TabView.OnItemIconTextSelectListener {

        public PageAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = mFragmentMap.get(position);

            if (fragment == null) {
                switch (position) {
                    case 0:
                        fragment = new CaptureStatus();
                        break;
                    case 1:
                        fragment = new AboutMe();
                        break;
                }
                mFragmentMap.put(position, fragment);
            }
            return fragment;
        }

        @Override
        public int[] onIconSelect(int position) {
            int[] icons = new int[2];
            icons[0] = mTabSelect[position];
            icons[1] = mTabNormal[position];
            return icons;
        }

        @Override
        public String onTextSelect(int position) {
            return mTabText[position];
        }

        @Override
        public int getCount() {
            return mTabItemCount;
        }
    }
}
